package com.maimao.upload.utils;

import com.maimao.core.constant.Constants;
import com.maimao.core.enums.SizeUnit;
import org.apache.commons.lang3.StringUtils;

/**
 * @author MaoLin Wang
 * @date 2020/12/17 10:07 下午
 */
public class UploadUtils {
    public static String getExtension(String originalFilename, String s) {
        return StringUtils.substringAfterLast(originalFilename, s);
    }

    public static String getThumbUrlFromFullPath(String fullPath, Integer width, Integer height) {
        //名称
        String fullName = StringUtils.substringBeforeLast(fullPath, Constants.DOT);
        //拓展名
        String extension = UploadUtils.getExtension(fullPath, Constants.DOT);
        if (!StringUtils.isAnyEmpty(fullName, extension)) {
            //缩略图
            return fullName + Constants.UNDER_LINE + width + "x" + height + Constants.DOT + extension;
        }
        return Constants.EMPTY;
    }

    public static String getFullPathFromThumb(String thumb) {
        if (StringUtils.isEmpty(thumb)) {
            return Constants.EMPTY;
        }
        String fullPath = StringUtils.substringBeforeLast(thumb, Constants.UNDER_LINE_STR) + Constants.DOT + StringUtils.substringAfterLast(thumb, ".");

        return fullPath;
    }
    public static boolean checkFileSize(Long len, int limit, SizeUnit unit) {
        double fileSize = 0;
        if (SizeUnit.B.equals(unit)) {
            fileSize = (double) len;
        } else if (SizeUnit.KB.equals(unit)) {
            fileSize = (double) len / 1024;
        } else if (SizeUnit.MB.equals(unit)) {
            fileSize = (double) len / 1048576;
        } else if (SizeUnit.GB.equals(unit)) {
            fileSize = (double) len / 1073741824;
        }
        if (fileSize > limit) {
            return false;
        }
        return true;
    }

}
